home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / progjrn / pj_7_2.arc / WINDEV2.ARC / WINAUXNT.C < prev    next >
Text File  |  1988-12-04  |  6KB  |  215 lines

  1. /* 
  2.  * Winaux initialization module
  3.  *
  4.  * Written by Bill Hall
  5.  * 3665  Benton Street, #66
  6.  * Santa Clara, CA 95051
  7.  *
  8.  */
  9.  
  10. #define NOCOMM
  11. #include <stdlib.h>
  12. #include <windows.h>
  13. #include <ttycls.h>
  14. #include "winaux.h"
  15.  
  16. /* local function declarations */
  17. static BOOL NEAR RegisterWindowClass(HANDLE);
  18. static void NEAR getcrect(HANDLE hInstance);
  19. static BOOL NEAR MakeAndShowMainWnd(HANDLE, int);
  20.  
  21. /* initial window size rectangle */
  22. static RECT crect;
  23.  
  24. /* enter here to initialize the program */
  25. BOOL FAR InitProgram(hInstance,hPrevInstance, lpszCmdLine, cmdShow)
  26. HANDLE hInstance, hPrevInstance;
  27. LPSTR lpszCmdLine;
  28. int cmdShow;
  29. {
  30.  
  31.   /* if first instance, do these things */
  32.     if (hPrevInstance == NULL) {
  33.         LoadString(hInstance,IDS_ICONSTRING,(LPSTR)szIconTitle,
  34.                 sizeof(szIconTitle));
  35.     LoadString(hInstance,IDS_HWND,(LPSTR)szhWnd,sizeof(szhWnd));
  36.     if (!RegisterWindowClass(hInstance))
  37.         return FALSE;
  38.     }
  39.     else
  40.       return FALSE;
  41.   /* no additional instances allowed */
  42.  
  43.   /* read the rectangle for create window */
  44.     getcrect(hInstance);
  45.  
  46.   /* create the window */
  47.     if (!MakeAndShowMainWnd(hInstance,cmdShow))
  48.     return FALSE;
  49.  
  50.   /* write the handle to win.ini */
  51.     if (!SetWinIni(MWnd.hWnd))
  52.     return FALSE;
  53.  
  54.   /* show success */
  55.     return TRUE;
  56. }
  57.  
  58. /* register the windows class */
  59. static BOOL near RegisterWindowClass(hInstance)
  60. HANDLE hInstance;
  61. {
  62.  
  63.     PWNDCLASS pWndClass;
  64.     HANDLE hTemp;
  65.  
  66.   /* get the application name from the resources */
  67.     LoadString(hInstance,IDS_APPNAME,(LPSTR)szAppName,sizeof(szAppName));
  68.  
  69.   /* make space for the WNDCLASS structure in the heap */
  70.     hTemp = LocalAlloc(LPTR,sizeof(WNDCLASS));
  71.     pWndClass = (PWNDCLASS)LocalLock(hTemp);
  72.     
  73.   /* enter data */
  74.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);    /* stock cursor */
  75.     pWndClass->hIcon = NULL;                /* no icon */
  76.     pWndClass->lpszMenuName = (LPSTR)szAppName;        /* this menu */
  77.     pWndClass->lpszClassName = (LPSTR)szAppName;    /* class name */
  78.     pWndClass->hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); /* background */
  79.     pWndClass->hInstance = hInstance;            /* instance */
  80.     pWndClass->style = CS_VREDRAW | CS_HREDRAW;        /* style */
  81.     pWndClass->lpfnWndProc = MainWndProc;    /* pointer to wind proc */
  82.     pWndClass->cbWndExtra = sizeof(PTTYWND);   /* space for ptr to MWnd */
  83.  
  84.   /* if failure, return FALSE */
  85.     if (!RegisterClass((LPWNDCLASS)pWndClass))
  86.     return FALSE;
  87.  
  88.   /* success, so free up heap data and show ok */
  89.     LocalUnlock(hTemp);
  90.     LocalFree(hTemp);
  91.     return TRUE;
  92. }
  93.  
  94. /* get the user's window size or load and write default values */
  95. static void NEAR getcrect(HANDLE hInstance)
  96. {
  97.  
  98.     char buf[20];
  99.     char keystr[20];
  100.     short temp;
  101.  
  102.     LoadString(hInstance,IDS_X,(LPSTR)keystr,sizeof(keystr));
  103.     temp = GetProfileInt((LPSTR)szAppName, (LPSTR)keystr, -1);
  104.     if (temp <= 0) {
  105.     crect.left = CW_USEDEFAULT;
  106.     if (temp < 0)
  107.         WriteProfileString((LPSTR)szAppName, (LPSTR)keystr,
  108.                  (LPSTR)itoa(crect.left, buf, 10));
  109.     }
  110.     else
  111.     crect.left = temp;
  112.  
  113.     LoadString(hInstance,IDS_Y,(LPSTR)keystr,sizeof(keystr));
  114.     temp = GetProfileInt((LPSTR)szAppName, (LPSTR)keystr, -1);
  115.     if (temp <= 0) {
  116.     crect.top = 0;
  117.     if (temp < 0)
  118.         WriteProfileString((LPSTR)szAppName, (LPSTR)keystr,
  119.                  (LPSTR)itoa(crect.top, buf, 10));
  120.     }
  121.     else
  122.     crect.top = temp;
  123.  
  124.     LoadString(hInstance,IDS_CX,(LPSTR)keystr,sizeof(keystr));
  125.     temp = GetProfileInt((LPSTR)szAppName, (LPSTR)keystr, -1);
  126.     if (temp <= 0) {
  127.     crect.right = CW_USEDEFAULT;
  128.     if (temp < 0)
  129.         WriteProfileString((LPSTR)szAppName, (LPSTR)keystr,
  130.                  (LPSTR)itoa(crect.right, buf, 10));
  131.     }
  132.     else
  133.     crect.right = temp;
  134.  
  135.     LoadString(hInstance,IDS_CY,(LPSTR)keystr,sizeof(keystr));
  136.     temp = GetProfileInt((LPSTR)szAppName, (LPSTR)keystr, -1);
  137.     if (temp <= 0) {
  138.     crect.bottom = 0;
  139.     if (temp < 0)
  140.         WriteProfileString((LPSTR)szAppName, (LPSTR)keystr,
  141.                  (LPSTR)itoa(crect.bottom, buf, 10));
  142.     }
  143.     else
  144.     crect.bottom = temp;
  145.  
  146. }
  147.  
  148. /* create a window of the class registered above */
  149. static BOOL NEAR MakeAndShowMainWnd(hInstance, cmdShow)
  150. HANDLE hInstance;
  151. int cmdShow;
  152. {
  153.  
  154.     char szWinTitle[30];
  155.  
  156.   /* get the text for the title */
  157.     LoadString(hInstance, IDS_WINTITLE, (LPSTR)szWinTitle,sizeof(szWinTitle));
  158.  
  159.   /* create the window */
  160.     MWnd.hWnd = CreateWindow((LPSTR)szAppName,    /* class */
  161.                  (LPSTR)szWinTitle,    /* title string */
  162.                  WS_OVERLAPPEDWINDOW,    /* style */
  163.                  crect.left,   /* values obtained from getcrect */
  164.                  crect.top,
  165.                  crect.right,
  166.                  crect.bottom,
  167.                  (HWND)NULL,    /* no parent */
  168.                  (HMENU)NULL,    /* class menu */
  169.                  (HANDLE)hInstance,    /* instance */
  170.                  (LPSTR)&MWnd);    /* ptr to window structure */
  171.  
  172.   /* if successful, display the window */
  173.     if (MWnd.hWnd && MWnd.hVidBuf) {
  174.     ShowWindow(MWnd.hWnd, cmdShow);
  175.         UpdateWindow(MWnd.hWnd);
  176.     return TRUE;
  177.     }
  178.   /* return failure */
  179.     return FALSE;
  180. }
  181.  
  182. /* this routine is called upon receipt of the WM_CREATE message */
  183. void WndCreate(hWnd, lParam)
  184. HWND hWnd;
  185. LONG lParam;
  186. {
  187.  
  188.     HDC hDC;
  189.     TEXTMETRIC TM;
  190.     short width, height, cwidth, cheight;
  191.     LPCREATESTRUCT pCS;
  192.     PTTYWND pMWnd;
  193.  
  194.   /* we are going to create a buffer for a window this large */
  195.     width = GetSystemMetrics(SM_CXFULLSCREEN);
  196.     height = GetSystemMetrics(SM_CYFULLSCREEN);
  197.  
  198.   /* get the font size */
  199.     hDC = GetDC(hWnd);
  200.     GetTextMetrics(hDC, &TM);
  201.     cwidth = TM.tmAveCharWidth;
  202.     cheight = TM.tmHeight + TM.tmExternalLeading;
  203.     ReleaseDC(hWnd, hDC);
  204.  
  205.   /* here we retrieve the pointer to our local window stucture */
  206.     pCS = (LPCREATESTRUCT)lParam;
  207.     pMWnd = (PTTYWND)LOWORD(pCS->lpCreateParams);
  208.  
  209.   /* initialize the extra data to the pointer to the TWnd structure */
  210.     SetWindowWord(hWnd,0,(WORD)pMWnd);
  211.  
  212.   /* now create the text buffer and set some defaults */
  213.     InitTTYWindow(pMWnd,0,0,width,height,cwidth,cheight,FALSE,TRUE,TRUE,0x7f);
  214. }
  215.